package com.proton.carepatchtemp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.proton.carepatchtemp.R;
import com.proton.carepatchtemp.adapterholder.PrfChooseHolder;
import com.proton.carepatchtemp.constant.Codes;
import com.proton.carepatchtemp.database.table.Profile;
import com.proton.carepatchtemp.utils.eventbus.MainEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * Created by MoonlightSW on 2016/12/5.
 */

public class PrfChooseAdapter extends RecyclerView.Adapter<PrfChooseHolder> {

    private List<Profile> mProfiles;
    private LayoutInflater mInflater;

    public PrfChooseAdapter(Context context, List profiles) {
        this.mProfiles = profiles;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public PrfChooseHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = mInflater.inflate(R.layout.item_choose_profile, parent, false);
        return new PrfChooseHolder(view);
    }

    @Override
    public void onBindViewHolder(PrfChooseHolder holder, int position) {
        holder.mSdvProfileAvatar.setImageURI(mProfiles.get(position).getAvatar());
        holder.mTvName.setText(mProfiles.get(position).getRealname());
        if (!TextUtils.isEmpty(mProfiles.get(position).getBirthday())) {
            holder.mTvAge.setText(mProfiles.get(position).getAge());
        }
        holder.mLnlChooseProfile.setOnClickListener(view -> {
            EventBus.getDefault().post(new MainEvent(Codes.PRF_CHOOSE_PRF, mProfiles.get(position)));
        });
    }

    @Override
    public int getItemCount() {
        return null == mProfiles ? 0 : mProfiles.size();
    }
}
